import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

np.random.seed(666)
x = np.random.uniform(low=-3, high=3, size=100)
X = x.reshape(-1, 1)

y = 0.5* x**2 + x + 2 + np.random.normal(0, 1, size=100)

# estimator = LinearRegression()
# estimator.fit(X, y)
# y_predict = estimator.predict(X)

# plt.scatter(x, y)
# plt.plot(x, y_predict, color='red')
# plt.show()

# 欠拟合
# from sklearn.metrics import mean_squared_error
# error = mean_squared_error(y, y_predict)
# print(error)


X2 = np.hstack([X, X**2])
estimator2 = LinearRegression()
estimator2.fit(X2, y)
y_predict2 = estimator2.predict(X2)

plt.scatter(x, y)
plt.plot(np.sort(x), y_predict2[np.argsort(x)], color='red')
plt.show()